package study.queue;

import java.util.Scanner;

public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleArray queue = new CircleArray(4);
        Scanner sc = new Scanner(System.in);
        char key = ' ';
        Boolean loop = true;

        while (loop){
            System.out.println("============== 菜单 ==============");
            System.out.println("e（enQueue）：进队");
            System.out.println("d（deQueue）：出队");
            System.out.println("f（getFront）：查看对头");
            System.out.println("s（showQueue）：显示队列");
            System.out.println("c（clearQueue）：清除队列");
            System.out.println("============================");
            System.out.print("请输入字母选择菜单：");
            key = sc.next().charAt(0);
            switch (key){
                case 'e':
                    System.out.print("请输入进队的数据：");
                    int data = sc.nextInt();
                    queue.enQueue(data);
                    break;
                case 'd':

                    try {
                        int deQueueData = queue.deQueue();
                        System.out.println("出队的数据是：" + deQueueData);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'f':
                    try {
                        int front = queue.getFront();
                        System.out.println("当前对头数据：" + front);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 's':
                    System.out.println("显示队列：");
                    queue.showQueue();
                    break;
                case 'c':
                    queue.clearQueue(4);
                    break;
                default:
                    System.out.println("请输入正确的指令！");
                    break;
            }
        }

        sc.close();


    }



}

class CircleArray{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;


    //构造器
    public CircleArray(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.arr = new int[maxSize];
    }

    //判断队列是否满
    public Boolean isFull(){
        return (rear + 1) % maxSize == front;
    }


    //判断队列是否空
    public Boolean isEmpty(){
        return rear == front;
    }

    //进队
    public void enQueue(int data){
        if (isFull()){
            System.out.println("队列已满，不能进队。");
            return;
        }
        arr[rear] = data;
        //尾指针的变化
        rear = (rear + 1) % maxSize;

    }


    //出队
    public int deQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列数据为空。");
        }
        int value = arr[front];
        //头指针front的变化
        front = (front + 1) % maxSize;
        return value;
    }


    //求出当前队列中有效数据的个数
    public int getValidData(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空，没有数据。");
            return;
        }
        for (int i = front; i < front + getValidData(); i++) {
            System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]);
        }

    }


    //取出队头的元素
    public int getFront() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }

    //清空队列
    public void clearQueue(int arrMaxSize){
        arr = new int[arrMaxSize];
        this.maxSize = arrMaxSize;
        this.front = 0;
        this.rear = 0;
    }
}



